package com.atguigu.ssyx.common.result;

import lombok.Data;

/**
 * ClassName: Result
 * Package: com.atguigu.ssyx.common.result
 * Description:
 *
 * @Author: 比特张
 * @Create: 2023/7/12 - 17:06
 * @Version: v1.0
 */
@Data
public class Result<T> {

    //状态码
    private Integer code;
    //信息
    private String message;
    //数据
    private T data;

    //构造私有化
    private Result() {
    }

    //设置数据,返回对象的方法
    public static <T> Result<T> build(T data, ResultCodeEnum resultCodeEnum) {
        //创建Resullt对象，设置值，返回对象
        Result<T> result = new Result<>();
        //判断返回结果中是否需要数据
        if (data != null) {
            //设置数据到result对象
            result.setData(data);
        }
        //设置其他值
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        //返回设置值之后的对象
        return result;
    }

    public static Result build(ResultCodeEnum resultCodeEnum) {
        Result result = new Result<>();
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }

    /**
     * 自定义异常类的方法
     *
     * @param code
     * @param message
     * @return
     */
    public static Result build(Integer code, String message) {
        Result result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    //成功的方法
    public static <T> Result<T> ok(T data) {
        Result<T> result = build(data, ResultCodeEnum.SUCCESS);
        return result;
    }

    public static Result ok() {
        Result result = build(ResultCodeEnum.SUCCESS);
        return result;
    }

    //失败的方法
    public static <T> Result<T> fail(T data) {
        return build(data, ResultCodeEnum.FAIL);
    }

    public static Result fail() {
        Result result = build(ResultCodeEnum.FAIL);
        return result;
    }
}